<template>
  <el-dropdown
      class = "avatar-container">
    <div class = "avatar-wrapper">
      <img :src = "avatar" class = "user-avatar">
      <i class = "el-icon-caret-bottom"/>
    </div>
    <template #dropdown>
      <el-dropdown-menu>
        <router-link to = "/">
          <el-dropdown-item>首页</el-dropdown-item>
        </router-link>
        <router-link to = "/profile/index">
          <el-dropdown-item>个人设置</el-dropdown-item>
        </router-link>
        <el-dropdown-item divided @click = "logout">
          <span style = "display: block">退出登录</span>
        </el-dropdown-item>
      </el-dropdown-menu>
    </template>
  </el-dropdown>
</template>

<script lang = "ts">
import { defineComponent } from 'vue'
import avatar from '@/assets/logo.png'
import { useStore } from '@/store'
import { PASSWORD_LOGIN } from '@/store/modules/methods-type'
import { useMessage } from '@/hooks/useMessage'

export default defineComponent({
  name: 'Avatar',
  setup() {
    const store = useStore(),
        message = useMessage()
    const logout = () => {
      store.dispatch(`user/${PASSWORD_LOGIN}`).then(() => {
        message.success('退出登录')
        window.location.reload()
      })
    }

    return {
      avatar,
      logout
    }
  }
})
</script>

<style lang = "scss" scoped>
.avatar-container {
  margin-right: 30px;

  .avatar-wrapper {
    margin-top: 5px;

    .user-avatar {
      width: 30px;
      height: 30px;
      border-radius: 10px;
      cursor: pointer;
    }

    .el-icon-caret-bottom {
      cursor: pointer;
      font-size: 12px;
    }
  }
}
</style>
